home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DDJ0992.ARJ / HRTTEST.C < prev    next >
C/C++ Source or Header  |  1991-12-16  |  6KB  |  253 lines

  1. /*
  2. **    hrttest.c -    hrtime test program
  3. */
  4.  
  5. /*
  6. ** Copyright notice begins here:
  7. **    Copyright (c) 1991 by Thomas A. Roden
  8. **    All Rights Reserved (with one exception).
  9. **    The right to freely distribute this source and any executable code
  10. **    it creates is granted, provided that this copyright notice is 
  11. **    included in the source.
  12. **
  13. **    It is requested that the author's name (Thomas A. Roden) be included
  14. **    in the acknowledgements of any product including this code, but this
  15. **    request is in no way legally binding.
  16. ** Copyright notice ends here:
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21.  
  22. typedef    unsigned long    U32;
  23.  
  24. /*
  25. **    constants for ticklet to second conversions
  26. **    ticklet to microsecond:    error < 1.3e-07 (one part in seven million)
  27. **        max out value 40,904,449
  28. **    microsecond to ticklet:    error < 1.3e-07 (one part in seven million)
  29. **        max out value 48,806,446
  30. **    ticklet to millisecond:    error < 1.3e-07 (one part in seven million)
  31. **        max out value 327,235
  32. **    millisecond to ticklet:    error < 1.3e-07 (one part in seven million)
  33. **        max out value 390,450,852
  34. */
  35. #define    T2USEC_NUMERATOR    88
  36. #define    T2USEC_DENOMINATOR    105
  37. #define    T2USEC_MAX_IN        48806445
  38.  
  39. #define    USEC2T_NUMERATOR    105
  40. #define    USEC2T_DENOMINATOR    88
  41. #define    USEC2T_MAX_IN        40904450
  42.  
  43. #define    T2MSEC_NUMERATOR    11
  44. #define    T2MSEC_DENOMINATOR    13125
  45. #define    T2MSEC_MAX_IN        390451572
  46.  
  47. #define    MSEC2T_NUMERATOR    13125
  48. #define    MSEC2T_DENOMINATOR    11
  49. #define    MSEC2T_MAX_IN        327235
  50.  
  51. #define T2USEC(i)    (ratio_conversion((i), T2USEC_NUMERATOR, \
  52.                     T2USEC_DENOMINATOR, T2USEC_MAX_IN))
  53.  
  54. #define USEC2T(i)    (ratio_conversion((i), USEC2T_NUMERATOR, \
  55.                     USEC2T_DENOMINATOR, USEC2T_MAX_IN))
  56.  
  57. #define T2MSEC(i)    (ratio_conversion((i), T2MSEC_NUMERATOR, \
  58.                     T2MSEC_DENOMINATOR, T2MSEC_MAX_IN))
  59.  
  60. #define MSEC2T(i)    (ratio_conversion((i), MSEC2T_NUMERATOR, \
  61.                     MSEC2T_DENOMINATOR, MSEC2T_MAX_IN))
  62.  
  63.  
  64. extern U32 far    hrtime(void);
  65. extern void far    hrt_open(void);
  66. extern void far    hrt_close(void);
  67.  
  68.  
  69. /*
  70. **    ratio_conversion -    transform a value from one unit to another
  71. **                        using a ratio, with as little overflow
  72. **                        as possible
  73. */
  74. U32    ratio_conversion(U32 in_value, U32 numerator, U32 denominator, 
  75.             U32 max_input)
  76. {
  77.     U32    retVal;
  78.  
  79.     if(in_value>max_input)
  80.         in_value = max_input;
  81.  
  82.     retVal = ((in_value*numerator) + (denominator>>1)) / denominator;
  83.  
  84.     return(retVal);
  85. }
  86.  
  87.  
  88. /*
  89. **    small_self_time -    time a small loop to attempt to generate minimum 
  90. **                intervals and some larger intervals
  91. */
  92. void    small_self_time(U32 num_tries)
  93. {
  94.     U32    last_time;
  95.     U32    biggest_time;
  96.     U32    smallest_time;
  97.     U32    big_this;
  98.     U32    big_last;
  99.     U32    small_this;
  100.     U32    small_last;
  101.     U32    i;
  102.  
  103.     hrt_open();
  104.  
  105.     biggest_time = 0;
  106.     smallest_time = 0xFFFFFFFF;
  107.     big_this = 0;
  108.     big_last = 0;
  109.     last_time = hrtime();
  110.     for(i=0; i<num_tries; i++)
  111.     {
  112.         U32    this_time;
  113.         U32    this_diff;
  114.  
  115.         this_time = hrtime();
  116.         this_diff = this_time - last_time;
  117.         if(this_diff>biggest_time)
  118.         {
  119.             biggest_time = this_diff;
  120.             big_this = this_time;
  121.             big_last = last_time;
  122.         }
  123.         if(this_diff<smallest_time)
  124.         {
  125.             smallest_time = this_diff;
  126.             small_this = this_time;
  127.             small_last = last_time;
  128.         }
  129.         last_time = this_time;
  130.     }
  131.  
  132.     hrt_close();
  133.  
  134.     printf("After %lu tries:\n", num_tries);
  135.     printf("Largest interval 0x%lx ticklets, smallest interval 0x%lx ticklets\n", 
  136.             biggest_time, smallest_time);
  137.     printf("Big this: 0x%lx, Big last: 0x%lx\n", 
  138.             big_this, big_last);
  139.     printf("Small this: 0x%lx, Small last: 0x%lx\n", 
  140.             small_this, small_last);
  141.     printf("In microseconds:\n");
  142.     printf("Largest interval %ld uSeconds, smallest interval %ld uSeconds\n", 
  143.             T2USEC(biggest_time), T2USEC(smallest_time));
  144.     printf("In milliseconds:\n");
  145.     printf("Largest interval %ld milliseconds, smallest interval %ld milliseconds\n", 
  146.             T2MSEC(biggest_time), T2MSEC(smallest_time));
  147. }
  148.  
  149. /*
  150. **    print_self_time -    time a printing loop to attempt to generate 
  151. **                moderate intervals
  152. */
  153. void    print_self_time(void)
  154. {
  155.     U32    last_time;
  156.     int    i;
  157. /*
  158. **    initialize timing system
  159. */
  160.     hrt_open();
  161.  
  162.     last_time = hrtime();
  163.     for(i=0; i<10; i++)
  164.     {
  165.         U32    this_time;
  166.         U32    this_diff;
  167.  
  168.         this_time = hrtime();
  169.         this_diff = this_time - last_time;
  170.         last_time = this_time;
  171.         printf("Loop #%d, interval %ld uSeconds\n", 
  172.                 i, T2USEC(this_diff));
  173.     }
  174.  
  175.     hrt_close();
  176. }
  177.  
  178. /*
  179. **    print_delay -    printing at fixed delays to demonstrate
  180. **                interval serviceing
  181. */
  182. void    print_delay(void)
  183. {
  184.     U32    last_time;
  185.     U32    last_adj_time;
  186.     U32    requested_diff;
  187.     int    i;
  188. /*
  189. **    initialize timing system
  190. */
  191.     hrt_open();
  192. /*
  193. **    set requested interval at 1,000,000 microseconds (1 second)
  194. */
  195.     requested_diff = USEC2T(1000000);
  196.     last_time = hrtime();
  197.     last_adj_time = last_time;
  198.     for(i=0; i<10; i++)
  199.     {
  200.         U32    this_time;
  201.         U32    this_diff;
  202.  
  203.         do
  204.         {
  205.             this_time = hrtime();
  206.             this_diff = this_time - last_adj_time;
  207.         } while(this_diff<requested_diff);
  208.         printf("Loop #%d, attempted %ld uSeconds, achieved %ld uSeconds\n", 
  209.                 i, T2USEC(requested_diff), T2USEC(this_time-last_time));
  210. /*
  211. **    calculating last_adj_time instead of using last_time gives longer
  212. **    term stability.  otherwise overshoots accumulate.
  213. */
  214.         last_time = this_time;
  215.         last_adj_time += requested_diff;
  216.     }
  217.  
  218.     hrt_close();
  219. }
  220.  
  221.  
  222. int    main(int argc, char *argv[])
  223. {
  224.     U32    num_tries;
  225.     int    test_mask;
  226.  
  227.     test_mask = 0x07;
  228.     num_tries = 1000;
  229.  
  230.     if(3==argc)
  231.     {
  232.         test_mask = (int)(strtoul(argv[1], NULL, 0));
  233.         num_tries = strtoul(argv[2], NULL, 0);
  234.     }
  235.     else if(2==argc)
  236.     {
  237.         test_mask = (int)(strtoul(argv[1], NULL, 0));
  238.     }
  239.     else if(1!=argc)
  240.         printf("<usage>: hrttest [test_mask] [num_tries]\n");
  241.  
  242.     if(0!=(test_mask&0x01))
  243.         small_self_time(num_tries);
  244.  
  245.     if(0!=(test_mask&0x02))
  246.         print_self_time();
  247.  
  248.     if(0!=(test_mask&0x04))
  249.         print_delay();
  250.  
  251.     return(0);
  252. }
  253.